<template>
    <NavBar></NavBar>
    <ContentField>
     <h2 style="text-align:center;margin-bottom: 20px;">添加题集</h2>

     <form  @submit.prevent="addProSet">
        <div class="mb-3">
            <label for="setId" class="form-label">题集号(setId): </label>
            <input type="text" v-model="setId"  class="form-control" id="setId" required>
        </div>
        <div class="mb-3">
            <label for="createdBy" class="form-label">创建者(createdBy): </label>
            <span type="number" class="form-control" id="createdBy">{{ createdBy }}</span>
        </div>
        <span style="color:red; text-align:center; display:block;">{{ error_message }}</span>
        <div align="center">
            <button type="submit" class="btn btn-primary" style="width:20%;">添加</button>
        </div>
    </form>

    </ContentField>
 </template>
 

 <script>
 
 import NavBar from "@/components/teacher/NavBar.vue";
 import ContentField from '@/components/commons/ContentField.vue';
 import {ref} from 'vue';
 import router from '@/router/index';
 import $ from 'jquery';
 import {useStore} from 'vuex';

 export default{
     name: 'addSet',
     components: {
         NavBar,
         ContentField,
     },
     setup(){
        const store = useStore();
        let setId = ref('');
        let createdBy = ref('');
        let error_message = ref('');

        createdBy.value = store.state.user.id;


        const addProSet = () => {

            $.ajax({  //通过TId获取所有题集
                url: 'http://localhost:8090/sls/proSet/addProSet',
                type: 'post',
                data: {
                    setId : setId.value,
                    createdBy: createdBy.value,
                },
                headers: {
                    Authorization: "Bearer " +store.state.user.token,
                },
                success(resp){
                    if(resp.error_message === "success"){
                       router.push({name: 'setManage'});
                    }
                    else{ 
                        error_message.value = resp.error_message;
                    }
                },
                error(resp){
                    console.log(resp);
                }
            });

        }

        return{
            addProSet,
            setId,
            createdBy,
            error_message,

        }

     }

 }
 
 </script>
 
 
 
 <style scoped>
 
 </style>